#!/usr/bin/env python
#coding:utf-8

from sqlobject import *

class MUser(SQLObject):
    name = UnicodeCol(length = 10)
    password = StringCol(length = 20)
    permission = IntCol(default = 1)
    
class MCategory(SQLObject):
    name = UnicodeCol()
    shortname = StringCol(length = 10, default = None)
    
class MPartCategory(MCategory):
    pass
    
class MPartType(SQLObject):
    name = StringCol(length = 10)
    chinese = UnicodeCol(default = None)
    category = ForeignKey('MPartCategory', cascade = False)
    barcode = StringCol(default = None)
    lstdconsangle = FloatCol()
    lconsangletoler = FloatCol(default = 0.0)
    lstdcambangle = FloatCol()
    lcambangletoler = FloatCol(default = 0.0)
    rstdconsangle = FloatCol()
    rconsangletoler = FloatCol(default = 0.0)
    rstdcambangle = FloatCol()
    rcambangletoler = FloatCol(default = 0.0)
    lenmin = FloatCol(default = None)
    lenmax = FloatCol(default = None)
    lendiffmin = FloatCol(default = None)
    lendiffmax = FloatCol(default = None)
    
    def _set_chinese(self, value):
        if isinstance(value, (str,unicode)):
            if len(value) < 1:
                value = None
        self._SO_set_chinese(value)
    
class MEntity(SQLObject):
    name = UnicodeCol()
    code = StringCol()
    describe = UnicodeCol(default = None)
    
class MPart(SQLObject):
    parttype = ForeignKey('MPartType')
    barcode = StringCol(default = None)
    createtime = DateTimeCol(default = DateTimeCol.now)
    machinebegintime = DateTimeCol(default = None)
    machineendtime = DateTimeCol(default = None)
    lconsangle = FloatCol()      # 4个 角度测量结果，左/右 前束角，后倾角
    lconsresult = StringCol(length = 6)
    lcambangle = FloatCol()
    lcambresult = StringCol(length = 6)
    rconsangle = FloatCol()
    rconsresult = StringCol(length = 6)
    rcambangle = FloatCol()
    rcambresult = StringCol(length = 6)
    length = FloatCol(default = None)
    lengthresult = StringCol(length = 6, default = None)
    lendiff = FloatCol(default = None)
    lendiffresult = StringCol(length = 6, default = None)
    torque1 = FloatCol(default = None)   #4个拧紧结果,角度，扭矩，结果
    angle1 = FloatCol(default = None)
    result1 = StringCol(length = 6, default = None)
    torque2 = FloatCol(default = None)
    angle2 = FloatCol(default = None)
    result2 = StringCol(length = 6, default = None)
    torque3 = FloatCol(default = None)
    angle3 = FloatCol(default = None)
    result3 = StringCol(length = 6, default = None)
    torque4 = FloatCol(default = None)
    angle4 = FloatCol(default = None)
    result4 = StringCol(length = 6, default = None)
    
class MCalibrationLog(SQLObject):
    createtime = DateTimeCol(default = DateTimeCol.now)
    pv1 = FloatCol()
    pv2 = FloatCol()
    pv3 = FloatCol()
    pv4 = FloatCol()
    pv5 = FloatCol()
    pv6 = FloatCol()
    
def Connect():
    conn_string = 'postgres://postgres:123456@localhost:5432/MIPW'
    conn = connectionForURI(conn_string)
    sqlhub.processConnection = conn
    return True

def Check():
    MEntity.createTable(ifNotExists = True)
    MUser.createTable(ifNotExists = True)
    MPartCategory.createTable(ifNotExists = True)
    MPartType.createTable(ifNotExists = True)
    MPart.createTable(ifNotExists = True)
    MCalibrationLog.createTable(ifNotExists = True)